package com.benhil.hrms.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.benhil.hrms.model.PersistentObject;
import com.benhil.hrms.util.JsonResponseContainer.Response;

public abstract class AbstractCrudController<DTO extends PersistentObject> {

	protected abstract Response getCreateResponse(DTO dto);
	protected abstract Response getReadResponse(int page, int start, int limit);
	protected abstract Response getUpdateResponse(DTO dto);
	protected abstract Response getDeleteResponse(DTO dto);
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
    public @ResponseBody Response create(@RequestBody DTO dto) {
		return getCreateResponse(dto);
    }
	
	@RequestMapping(value = "/view.json", method = RequestMethod.GET)
    public @ResponseBody Response read(
    		@RequestParam int page,
    		@RequestParam int start,
    		@RequestParam int limit) {
		
		return getReadResponse(page, start, limit);
    }

	@RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody Response update(@RequestBody DTO dto) {
		return getUpdateResponse(dto);
    }
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody Response delete(@RequestBody DTO dto) {
		return getDeleteResponse(dto);
    }

}
